package com.example.ele_guarantee_hh_pro.trigger;
import com.cloudcc.deveditor.core.CCObject;

import com.cloudcc.deveditor.trigger.TriggerEditor;
import com.example.ele_guarantee_hh_pro.comclass.EleQueryCustomer3;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/*管户小组*/
public class eleopp extends TriggerEditor {

    /*插入商机时生成name*/
    public void afterInsert(){

        String accountName = record_new.get("khmc") == null ? "" : record_new.get("khmc") + "";
        String id = record_new.get("id") == null ? "" : record_new.get("id") + "";
        String fk_psmc = record_new.get("fk_psmc") == null ? "" : record_new.get("fk_psmc") + "";
        String ownerid = record_new.get("ownerid") == null ? "" : record_new.get("ownerid") + "";
        String sqyy = record_new.get("sqyy") == null ? "" : record_new.get("sqyy") + "";
        String name = "[电子保函商机]：";


        if (!sqyy.equals("")){
            String findCcuser = "select jgfzr from ccuser where id ='" + ownerid + "' and is_deleted='0' ";
            List<CCObject> ccObjects = this.cqlQuery("", findCcuser);
            if (ccObjects.size() > 0) {
                String jgfzr = ccObjects.get(0).get("jgfzr") == null ? "" : ccObjects.get(0).get("jgfzr") + "";
                this.submitForApproval(id,jgfzr , "");
            }
        }


        if (!accountName.equals("")) {
            String findAccountName = "select name from Account where id = '" + accountName + "' ";
            List<CCObject> account = this.cqlQuery("Account", findAccountName);
            if (account.size() > 0) {
                name += account.get(0).get("name");
            }
            String find = "select frequece from ele_num where fk_psmc ='" + fk_psmc + "' and fk_account ='" + accountName + "' and is_deleted='0'";
            List<CCObject> ele_num = this.cqlQuery("ele_num", find);

            if (ele_num.size() > 0) {
                String frequece = ele_num.get(0).get("frequece") == null ? "" : ele_num.get(0).get("frequece") + "";
                String updateFrequeceSql = "update eleopp set frequence ='" + frequece + "' where id ='" + id + "' ";
                this.cqlQuery("eleopp", updateFrequeceSql);
            }
        }
        String sql = "update eleopp set name = '" + name + "' where id = '" + id + "' ";
        this.cqlQuery("eleopp", sql);


        //正式环境：电子保函客户的记录类型
        String AC_RCTYPE = "202385551266A597FF1B";
        String socialCreditCode = "";
        String RCODE = "0000";
        //关联客户
        String khmc = record_new.get("khmc") == null ? "" : record_new.get("khmc") + "";
        /*查询客户信息,有的话更改记录类型并且做关联*/
        String acc_sql = "select id,recordtype,invoiceTaxNo from Account where id='" + khmc + "' and is_deleted='0'";
        List<CCObject> account = this.cqlQuery("Account", acc_sql);
        if (account.size() > 0) {
            socialCreditCode = account.get(0).get("invoiceTaxNo") == null ? "" : account.get(0).get("invoiceTaxNo") + "";
            String update_acc_sql = "update Account set recordtype='" + AC_RCTYPE + "',follow_status='跟进中' where id='" + khmc + "'";
            //客户记录类型更改
            this.cqlQuery("Account", update_acc_sql);

        }
        /*查询会员信息*/
        EleQueryCustomer3 eqc2 = new EleQueryCustomer3(userInfo);
        net.sf.json.JSONObject params = new net.sf.json.JSONObject();
        net.sf.json.JSONObject data = new net.sf.json.JSONObject();
        params.put("invokeId", "");
        data.put("cusCertType", "1");
        data.put("cusCertNo", socialCreditCode);
        params.put("data", data);
        String rs = eqc2.sendPost("", params.toString());
        CCObject interface_record = new CCObject("interface_record");
        interface_record.put("datas", params);
        interface_record.put("url", "EleQueryCustomer");
        interface_record.put("requestDataLong", rs);
        this.insert(interface_record);
        net.sf.json.JSONObject result = net.sf.json.JSONObject.fromObject(rs);
        String resultCode = result.getString("resultCode");
        if (resultCode.equals(RCODE)) {
            net.sf.json.JSONObject info = result.getJSONObject("data");
            if (!info.isNullObject()) {
                String cusSource = info.get("cusSource") == null ? "" : info.get("cusSource") + "";
                String createTime = info.get("createTime") == null ? "" : info.get("createTime") + "";

                String findSource = "select fy from zcly where name ='"+cusSource+"' and is_deleted='0' " ;
                String source = "";
                List<CCObject> zcly = this.cqlQuery("zcly", findSource);
                if (zcly.size()>0){
                    source = zcly.get(0).get("fy")==null?"":zcly.get(0).get("fy")+"";
                }



                String updateSql ="update eleopp set accountTime='"+createTime+"',source='"+source+"' where id ='"+id+"' ";
                this.cqlQuery("eleopp",updateSql);
            }
        }

        String findCcuser = "select jgfzr from ccuser where id ='" + ownerid + "' and is_deleted='0' ";
        List<CCObject> ccObjects = this.cqlQuery("ccuser", findCcuser);
        if (ccObjects.size() > 0) {
            String jgfzr = ccObjects.get(0).get("jgfzr") == null ? "" : ccObjects.get(0).get("jgfzr") + "";
            String updateEleopp = "update eleopp set leader ='"+jgfzr+"' where id ='"+id+"' ";
            this.cqlQuery("eleopp",updateEleopp);
        }

    }

    public void afterUpdate(){

        String api_ele = "eleopp";
        String api_manage_group = "manage_group";
        String jieduan5 = "5-丢单";
        String spzt_pass = "审批成功";
        String lost_status = "已失效";
        String start_status = "已生效";
        String jieduan2 = "2-初次拜访";
        String jieduan3 = "3-客情调研";
        String YDD = "已丢单";

        //获取id
        String id = record_new.get("id") == null ? "" : record_new.get("id") + "";
        //阶段
        String jieduan = record_new.get("jieduan") == null ? "" : record_new.get("jieduan") + "";
        //关联客户
        String khmc = record_new.get("khmc") == null ? "" : record_new.get("khmc") + "";
        //审批状态
        String spzt = record_new.get("spzt") == null ? "" : record_new.get("spzt") + "";
        //旧的审批状态
        String spzt_old = record_old.get("spzt") == null ? "" : record_old.get("spzt") + "";
        //交易中心
        String fk_psmc = record_new.get("fk_psmc") == null ? "" : record_new.get("fk_psmc") + "";
        //商机所有人
        String ownerid = record_new.get("ownerid") == null ? "" : record_new.get("ownerid") + "";
        //商机名称
        String name = record_new.get("name") == null ? "" : record_new.get("name") + "";
        //近一年公招项目投标次数
        String one_year_num = record_new.get("one_year_num") == null ? "" : record_new.get("one_year_num") + "";
        //投标习惯
        String habbit = record_new.get("habbit") == null ? "" : record_new.get("habbit") + "";
        //投标策略
        String strategy = record_new.get("strategy") == null ? "" : record_new.get("strategy") + "";
        //保证金缴纳方式
        String pay_method = record_new.get("pay_method") == null ? "" : record_new.get("pay_method") + "";
        //投标决策人
        String decision = record_new.get("decision") == null ? "" : record_new.get("decision") + "";
        //投标经办人
        String tender = record_new.get("tender") == null ? "" : record_new.get("tender") + "";

        /*审批机构负责人*/
        String leader = record_new.get("leader") == null ? "" : record_new.get("leader") + "";

        String sqyy = record_new.get("sqyy") == null ? "" : record_new.get("sqyy") + "";

        Date date = new Date();
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sf2.format(date);





        if (!sqyy.equals("")&& spzt.equals("草稿中")){
            String findCcuser = "select jgfzr from ccuser where id ='" + ownerid + "' and is_deleted='0' ";
            List<CCObject> ccObjects = this.cqlQuery("", findCcuser);
            if (ccObjects.size() > 0) {
                String jgfzr = ccObjects.get(0).get("jgfzr") == null ? "" : ccObjects.get(0).get("jgfzr") + "";
                this.submitForApproval(id,jgfzr , "");
            }
        }

        //丢单之后的逻辑
        if ((!spzt_old.equals(spzt)) && spzt.equals(spzt_pass)) {
            this.cqlQuery(api_ele, "update eleopp set jieduan='" + jieduan5 + "',status1='" + YDD + "',lose_time='" + now + "',sqyy='主动丢单' where id='" + id + "'");
            String manager_sql = "select * from manage_group where fk_ccuser='" + ownerid + "' and fk_account='" + khmc + "'";

            List<CCObject> manage_group = this.cqlQuery("manage_group", manager_sql);
            if (manage_group.size() > 0) {
                for (CCObject cc :
                        manage_group) {
                    cc.put("ghzt", lost_status);
                    cc.put("lost_time", now);
                    this.update(cc);
                }

            }

            String updateAccountType = "update Account set follow_status ='待分配' where id ='"+khmc+"' ";
            this.cqlQuery("Account",updateAccountType);


        }

        //阶段2，升迁三阶段，所有字段填写完毕；更新管护小组状态为第三阶段
        if (jieduan.equals(jieduan2) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(one_year_num) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(habbit) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(strategy) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(pay_method) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(decision) &&
                org.apache.commons.lang3.StringUtils.isNotBlank(tender)) {

            String manager_sql = "update manage_group set ghzt='" + start_status + "',get_time='" + now + "'" +
                    "where fk_account='" + khmc + "' " +
                    "and is_deleted='0' " +
                    "and fk_ccuser='" + ownerid + "'";
            String eleopp3_sql = "update eleopp set jieduan='" + jieduan3 + "',timeThreeStart='"+now+"',timeTwoEnd='"+now+"' " +
                    "where id='" + id + "'";

            //更新三阶段
            this.cqlQuery(api_ele, eleopp3_sql);
            //更新管护小组状态
            this.cqlQuery(api_manage_group, manager_sql);



        }


    }
}
